package com.sky.MyApplicationTests;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.sky.RuoYiApplication;
import com.sky.merchant.domain.ResShop;
import com.sky.merchant.service.RedisShopService;
import com.sky.merchant.service.ResShopService;
import lombok.extern.slf4j.Slf4j;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.*;

@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest(classes = RuoYiApplication.class)
public class MyApplicationTests {
    @Before
    public void showBefore(){
        log.info("################## MyApplicationTests start ################# ");
    }

    @After
    public void showAfter(){
        log.info("################## MyApplicationTests end ################# ");
    }

    /***********************************ResShopMapper***************************************/
    @Autowired
    private ResShopService resShopService;

    @Autowired
    private RedisShopService redisService;
    @Test
    public void testGetAllShops() {
        List<ResShop> resShops = resShopService.getAllShops();
        for (ResShop resShop : resShops) {
            System.out.println(resShop);
        }
    }

    @Test
    public void testAddShop() {
        // 创建一个ResShop对象
        ResShop resShop = new ResShop();

        // 设置ResShop对象的属性
        resShop.setName("Test Shop"); // 设置店铺名称为"Test Shop"
        resShop.setAddress("123 Test Street, Test City"); // 设置店铺地址
        resShop.setContact("1234567890"); // 设置联系电话
        resShop.setCover("http://example.com/shop-cover.jpg"); // 设置店铺封面图片URL
        resShop.setDetail("This is a test shop for demonstration purposes."); // 设置店铺详情描述
        resShop.setStar(4); // 设置店铺评分为4星
        resShop.setOwnerId(100L); // 设置店主ID为100L
        resShop.setCreateBy("testUser"); // 设置创建者用户名或ID
        resShop.setCreateTime(new Date()); // 设置创建时间为当前时间
        resShop.setUpdateBy("testUser"); // 设置更新者用户名或ID
        resShop.setUpdateTime(new Date()); // 设置更新时间为当前时间

        // 调用resShopService的addShop方法并打印结果
        int result = resShopService.addShop(resShop);
        System.out.println("Add shop result: " + result);
    }
    @Test
    public void testRemoveShopById() {
        Long id = 1L;
        int result = resShopService.removeShopById(id);
        System.out.println("Remove shop result: " + result);
    }

    @Test
    public void testUpdateShop() {
        ResShop resShop = new ResShop();

        resShop.setId(8L);
        resShop.setName("Test Update Shop"); // 设置店铺名称为"Test Shop"
        resShop.setAddress("123 Test Street, Test City"); // 设置店铺地址
        resShop.setContact("1234567890"); // 设置联系电话
        resShop.setCover("http://example.com/shop-cover.jpg"); // 设置店铺封面图片URL
        resShop.setDetail("This is a test shop for demonstration purposes."); // 设置店铺详情描述
        resShop.setStar(4); // 设置店铺评分为4星
        resShop.setOwnerId(100L); // 设置店主ID为100L
        resShop.setCreateBy("testUser"); // 设置创建者用户名或ID
        resShop.setCreateTime(new Date()); // 设置创建时间为当前时间
        resShop.setUpdateBy("testUser"); // 设置更新者用户名或ID
        resShop.setUpdateTime(new Date()); // 设置更新时间为当前时间

        int result = resShopService.updateResShop(resShop);
        System.out.println(result);
    }

    @Test
    public void testGetShopById(){
        Long id = 6L;
        ResShop resShop = resShopService.getShopById(id);
        System.out.println(resShop);
    }
//    public void testGetShopById() throws JsonProcessingException {
//        Long id = 6L;
//        ResShop resShop;
//        resShop = redisService.getResShop(id);//KEYS resShop:*;get resShop:*
//        if (resShop != null) {
//            System.out.println("Get resshop in redis!");
//        }
//        else {
//            resShop = resShopService.getShopById(id);
//            if (resShop != null) {
//                redisService.cacheResShop(id, resShop);
//            }
//        }
//
//        System.out.println(resShop);
//    }


    /***********************************ResShopMapper***************************************/


    /***************************************动态SQL*****************************************/
    @Test
    public void testSelectShops() {
        // 创建一个参数Map
        Map<String, Object> params = new HashMap<>();

        // 测试查询所有店铺
        List<ResShop> shops = resShopService.selectShops(params);
        for (ResShop shop : shops) {
            System.out.println(shop.getName());
        }

        // 测试按名称查询
        params.clear();
        params.put("name", "Test Shop");
        List<ResShop> shopsByName = resShopService.selectShops(params);
        for (ResShop shop : shopsByName) {
            System.out.println(shop.getName());
        }

        // 测试按评分范围查询
        params.clear();
        params.put("starMin", 3);
        params.put("starMax", 5);
        List<ResShop> shopsByStar = resShopService.selectShops(params);
        for (ResShop shop : shopsByStar) {
            System.out.println(shop.getName());
        }

        // 测试按店主ID查询
        params.clear();
        params.put("name", "Test Shop");
        params.put("ownerId", 1L);
        List<ResShop> shopsByOwnerId = resShopService.selectShops(params);
        for (ResShop shop : shopsByOwnerId) {
            System.out.println(shop.getName());
        }
    }

    @Test
    public void testUpdateShops() {
        // 测试更新名称
        ResShop shopNameUpdate = new ResShop();
        shopNameUpdate.setId(1L);
        shopNameUpdate.setName("Updated Name");
        resShopService.updateShop(shopNameUpdate);

        // 测试更新星级
        ResShop shopStarUpdate = new ResShop();
        shopStarUpdate.setId(1L);
        shopStarUpdate.setStar(5);
        resShopService.updateShop(shopStarUpdate);

        // 测试更新店主ID
        ResShop shopOwnerIdUpdate = new ResShop();
        shopOwnerIdUpdate.setId(1L);
        shopOwnerIdUpdate.setOwnerId(2L);
        resShopService.updateShop(shopOwnerIdUpdate);

        // 测试同时更新多个字段
        ResShop shopMultipleUpdate = new ResShop();
        shopMultipleUpdate.setId(1L);
        shopMultipleUpdate.setName("Another Name");
        shopMultipleUpdate.setStar(4);
        shopMultipleUpdate.setOwnerId(3L);
        resShopService.updateShop(shopMultipleUpdate);
    }

    @Test
    public void testInsertShops() {
        // 创建一些ResShop对象作为测试数据
        ResShop shop1 = new ResShop();
        shop1.setName("Shop A");
        shop1.setStar(4);
        shop1.setOwnerId(1L);

        ResShop shop2 = new ResShop();
        shop2.setName("Shop B");
        shop2.setStar(5);
        shop2.setOwnerId(2L);

        List<ResShop> shops = Arrays.asList(shop1, shop2);

        // 调用被测试的方法
        resShopService.insertShops(shops);
    }

    /***************************************动态SQL*****************************************/
}
